package bb.lanxing.network;

import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.mapapi.SDKInitializer;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.util.Log;
import okhttp3.Call;
import rx.Observable;
import rx.Subscriber;

public class NetSubscribe implements Observable.OnSubscribe<String> {
    final private static String TAG = "NetSubscribe";
    private Call mCall;
    private okhttp3.Response response = null;

    public NetSubscribe(Call call) {
        this.mCall = call;
    }

    @Override
    public void call(Subscriber<? super String> subscriber) {
        try {
            okhttp3.Response execute = this.mCall.execute();
            this.response = execute;
            String string = this.response.body().string();
            if (execute.isSuccessful()) {
                Log.d(TAG, " response : " + this.response + " body : " + string);
                if (!subscriber.isUnsubscribed() || !this.mCall.isCanceled()) {
                    JSONObject jSONObject = new JSONObject(string);
                    int code = jSONObject.optInt("code");
                    if (code != 200) {
                        ApiException.checkResponseThrow(this.response.code(), string);
                    } else {
                        subscriber.onNext(string);
                    }
                }
            } else if (this.response.code() == 403) {
                if (App.getContext().isUserSignin()) {
                    App.getContext().cleanSigninState();
                }
                App.getContext().userSignin();

            } else if (this.response.code() == 504) {
                throw new ApiException(504, "获取数据失败,请检查网络连接");
            } else {
                ApiException.checkResponseThrow(this.response.code(), string);
            }
            subscriber.onCompleted();
        } catch (Exception e) {
            subscriber.onError(e);
            if (e instanceof NetSubscribe.ApiException apiException) {
                if (apiException.bodyCode == 401) {
                    App.getContext()
                            .showMessage(App.getContext().getResources().getString(R.string.common_text_unauthorize));
                    if (App.getContext().isUserSignin()) {
                        App.getContext().cleanSigninState();
                    }
                    App.getContext().userSignin();
                }
            }
        }
    }

    public okhttp3.Response getResponse() {
        return this.response;
    }

    public static class ApiException extends RuntimeException {
        private int bodyCode;
        private int code;

        public ApiException(int i, String str) {
            super(str);
            this.code = i;
        }

        ApiException(int i, int i2, String str) {
            super(str);
            this.code = i;
            this.bodyCode = i2;
        }

        public static ApiException newInstance(int i, String str) {
            if (str == null) {
                return null;
            }
            int i2 = 0;
            try {
                JSONObject jSONObject = new JSONObject(str);
                if (jSONObject.has(SDKInitializer.SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_MESSAGE)) {
                    str = jSONObject.getString(SDKInitializer.SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_MESSAGE);
                } else {
                    str = jSONObject.getString("msg");
                }
                if (jSONObject.has("code")) {
                    i2 = jSONObject.getInt("code");
                }
                return new ApiException(i, i2, str);
            } catch (JSONException unused) {
                return new ApiException(i, str);
            }
        }

        public static void checkResponseThrow(int i, String str) {
            ApiException newInstance = newInstance(i, str);
            if (newInstance == null) {
                return;
            }
            throw newInstance;
        }

        public int getCode() {
            return this.code;
        }

        public int getBodyCode() {
            return this.bodyCode;
        }
    }
}
